#!/bin/bash

# ==========================================
# AnyPay聚合支付平台 - 7张核心表一键部署脚本
# ==========================================

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 配置参数
DB_HOST=${DB_HOST:-"localhost"}
DB_PORT=${DB_PORT:-"3306"}
DB_NAME=${DB_NAME:-"anypay"}
DB_USER=${DB_USER:-"root"}

# 检查参数
if [ -z "$DB_PASSWORD" ]; then
    echo -e "${RED}错误: 请设置环境变量 DB_PASSWORD${NC}"
    echo "示例: export DB_PASSWORD=your_password"
    exit 1
fi

# SQL文件列表
SQL_FILES=(
    "01_t_service_provider.sql"
    "02_t_merchant_info.sql"
    "03_t_pay_channel_dict.sql"
    "04_t_pay_product_dict.sql"
    "05_t_payment_app_config.sql"
    "06_t_unified_channel_config.sql"
    "07_t_protocol_config.sql"
)

# 表说明
TABLE_DESCRIPTIONS=(
    "服务商表"
    "商户信息表" 
    "支付渠道字典表"
    "支付产品字典表"
    "支付应用配置表"
    "统一渠道配置表"
    "Protocol协议配置表"
)

echo -e "${BLUE}===========================================${NC}"
echo -e "${BLUE}AnyPay聚合支付平台 - 7张核心表部署脚本${NC}"
echo -e "${BLUE}===========================================${NC}"
echo ""

# 显示配置信息
echo -e "${YELLOW}部署配置:${NC}"
echo "  数据库主机: $DB_HOST:$DB_PORT"
echo "  数据库名称: $DB_NAME"
echo "  数据库用户: $DB_USER"
echo ""

# 检查MySQL连接
echo -e "${YELLOW}检查数据库连接...${NC}"
mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" -e "SELECT 1;" > /dev/null 2>&1
if [ $? -ne 0 ]; then
    echo -e "${RED}错误: 无法连接到数据库${NC}"
    exit 1
fi
echo -e "${GREEN}✓ 数据库连接成功${NC}"

# 创建数据库(如果不存在)
echo -e "${YELLOW}创建数据库(如果不存在)...${NC}"
mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" -e "CREATE DATABASE IF NOT EXISTS \`$DB_NAME\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" 2>/dev/null
echo -e "${GREEN}✓ 数据库准备完成${NC}"
echo ""

# 执行SQL文件
echo -e "${YELLOW}开始执行SQL文件...${NC}"
echo ""

success_count=0
total_count=${#SQL_FILES[@]}

for i in "${!SQL_FILES[@]}"; do
    sql_file="${SQL_FILES[$i]}"
    description="${TABLE_DESCRIPTIONS[$i]}"
    
    echo -e "${BLUE}[$((i+1))/$total_count] 正在执行: $sql_file${NC}"
    echo -e "        描述: $description"
    
    if [ ! -f "$sql_file" ]; then
        echo -e "${RED}        ✗ 文件不存在: $sql_file${NC}"
        continue
    fi
    
    # 执行SQL文件
    mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" < "$sql_file" 2>/dev/null
    
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}        ✓ 执行成功${NC}"
        ((success_count++))
    else
        echo -e "${RED}        ✗ 执行失败${NC}"
    fi
    echo ""
done

# 显示结果
echo -e "${BLUE}===========================================${NC}"
echo -e "${YELLOW}部署结果统计:${NC}"
echo "  总文件数: $total_count"
echo "  成功数量: $success_count"
echo "  失败数量: $((total_count - success_count))"

if [ $success_count -eq $total_count ]; then
    echo -e "${GREEN}🎉 所有表创建成功!${NC}"
else
    echo -e "${RED}⚠️  部分表创建失败，请检查错误信息${NC}"
fi

# 验证表创建结果
echo ""
echo -e "${YELLOW}验证表创建结果...${NC}"
mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" -e "
SELECT 
    TABLE_NAME as '表名',
    TABLE_ROWS as '记录数',
    ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024), 2) as '大小(MB)',
    TABLE_COMMENT as '说明'
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = '$DB_NAME' 
  AND TABLE_NAME IN (
    't_service_provider',
    't_merchant_info', 
    't_pay_channel_dict',
    't_pay_product_dict',
    't_payment_app_config',
    't_unified_channel_config',
    't_protocol_config'
  )
ORDER BY TABLE_NAME;
" 2>/dev/null

echo ""
echo -e "${BLUE}===========================================${NC}"
echo -e "${GREEN}部署完成!${NC}"
echo -e "${BLUE}===========================================${NC}"